{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1df6834f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用百度API进行OCR识别\n",
    "#使用百度API首先要获得acces_token\n",
    "import requests\n",
    "client_id = 'jEEiqT4qL445MlHnOp3CiMLg'\n",
    "client_secret = 'nzKErDcZrfVPCih8ux8RgG4kG7qHRYzM'\n",
    "api = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "url = api + \"grant_type=client_credentials&client_id=\" + client_id + \"&client_secret=\" + client_secret\n",
    "r = requests.get(url)\n",
    "data = (r.json())\n",
    "access_token = data[\"access_token\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fe954ba1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.1b3e7c0f1182391708abca957188b260.2592000.1654068959.282335-25946170'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c2a41635",
   "metadata": {},
   "outputs": [],
   "source": [
    "#获得token后，使用OCR识别\n",
    "import base64\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('文字识别.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\n",
    "    \"image\":img\n",
    "}\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "r = requests.post(request_url, data=params, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2d053308",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '在通用文字识别的基础上，提供更高精度的识别服务，支持更多语种识别（丹麦语、荷兰语、马来语、瑞典语、印尼语、波兰语、罗马尼亚'},\n",
       "  {'words': '语、土耳其语、希腊语、匈牙利语、泰语、越语、阿拉伯语、印地语及部分中国少数民族语言)，并将字库从1w+扩展到2W+,能识别所有常'},\n",
       "  {'words': '用字和大部分生僻字。'}],\n",
       " 'words_result_num': 3,\n",
       " 'log_id': 1521030734793351617}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "3c4953c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#网络图片文字识别\n",
    "params = {\n",
    "    \"url\":'https://pic3.zhimg.com/80/v2-85e2ee203a2dbe9250935b04ba296cfe_720w.jpg'\n",
    "}\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "r = requests.post(request_url, data=params, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f1ea5f52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '拍照'}, {'words': '文字识别'}, {'words': '知乎@郭富城的城'}],\n",
       " 'words_result_num': 3,\n",
       " 'log_id': 1521030794085375865}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fada13d4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
